<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js" type="text/javascript"></script> -->
<script type="text/javascript" src="../jquery-2.1.1.js"></script>
<title>重写事件对象</title>
</head>
<body>


<div id="aaron">
   <div>
     <p>点击重写事件对象</p>
   </div>
</div>
<ul></ul>

<script type="text/javascript"> 


function returnTrue() {
    return true;
}

function returnFalse() {
    return false;
}


//模拟出事件对象
//增加调用判断方法
var Event = function(src, props) {
    if (src && src.type) {
        this.originalEvent = src;
        this.type = src.type;
        this.isDefaultPrevented = src.defaultPrevented ||
            src.defaultPrevented === undefined &&
            src.returnValue === false ?
            returnTrue :
            returnFalse;
    } else {
        this.type = src;
    }
    this.timeStamp = src && src.timeStamp || jQuery.now();
    this[jQuery.expando] = true;
};

Event.prototype = {
    isDefaultPrevented: returnFalse,
    isPropagationStopped: returnFalse,
    isImmediatePropagationStopped: returnFalse,
    preventDefault: function() {
        var e = this.originalEvent;
        this.isDefaultPrevented = returnTrue;
        if (e && e.preventDefault) {
            e.preventDefault();
        }
    },
    stopPropagation: function() {
        var e = this.originalEvent;
        this.isPropagationStopped = returnTrue;
        if (e && e.stopPropagation) {
            e.stopPropagation();
        }
    },
    stopImmediatePropagation: function() {
        var e = this.originalEvent;
        this.isImmediatePropagationStopped = returnTrue;
        if (e && e.stopImmediatePropagation) {
            e.stopImmediatePropagation();
        }
        this.stopPropagation();
    }
};

function fix(event) {
    //原始事件引用
    var originalEvent = event;
    //鼠标事件对象的属性
    var props = "altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp view which button buttons clientX clientY offsetX offsetY pageX pageY screenX screenY toElement".split(" ");

    //创建事件对象
    var event = new Event(originalEvent);

    //混入事件属性
    i = props.length;
    while (i--) {
        prop = props[i];
        event[prop] = originalEvent[prop];
    }
    return event
}


 document.getElementById('aaron').addEventListener('click',function(e){
    e = fix(e)
    for(var i in e){
        show('属性'+i+' 值：' +e[i])
    }
},false)



function show(data) {
    $('ul').append('<li>' + data + '</li>')
}

</script>

</body>
</html>
